package com.ithao.controller;

import com.ithao.entity.PageResult;
import com.ithao.entity.QueryPageBean;
import com.ithao.entity.Result;
import com.ithao.pojo.Company;
import com.ithao.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ZhangHao
 * @version :1.0
 * @date :Created in 2020/2/7
 * @description : 企业控制器
 */
@RestController
@Slf4j
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    /**
     * 获取所有企业数据
     */
    @RequestMapping("findListAll")
    public Result findListAll() {
        try {
            log.info("findListAll");
            List<Company> companyList = companyService.findListAll();
            return new Result(true, "获取成功", companyList);
        } catch (Exception e) {
            log.error("findListAll", e);
            return new Result(false, "获取失败");
        }
    }

    /**
     * 分页获取企业数据
     */
//    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/getListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean == null) {
            queryPageBean = new QueryPageBean();
            queryPageBean.setCurrentPage(1);
            queryPageBean.setPageSize(10);
        }
        log.info("[企业分页列表] queryPageBean :{}", queryPageBean);
        PageResult pageResult = companyService.findPage(queryPageBean);
        return new Result(true, "获取企业分页列表成功", pageResult);

    }

    /**
     * 企业的启用禁用
     */
    @PostMapping("/isDis")
    public Result isDis(@RequestParam("id") Integer id) {
        log.info("[企业的启用禁用] id: {}", id);
        companyService.isDis(id);
        return new Result(true, "变更成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Company company,Integer[] industryIds) {
        log.info("[企业add] company:{}  industryIds:{}",company,industryIds);
        Boolean flag =companyService.add(company,industryIds);
        if (flag) {
            return new Result(true,"新增成功");
        }
        return new Result(false,"新增失败");
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        companyService.delete(id);
        return new Result(true,"删除成功");
    }
}
